package com.example.mvc.controller;

import com.example.mvc.bean.Book;
import com.example.mvc.bean.BookType;
import com.example.mvc.bean.User;
import com.example.mvc.service.BookBorrowService;
import com.example.mvc.service.BookService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/userBorrowBookServlet")
public class UserBorrowBookServlet extends HttpServlet {
 BookBorrowService bookBorrowService = new BookBorrowService();
 BookService bookService = new BookService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookId = req.getParameter("bookId");
        String userId = req.getParameter("userId");
        //借书
        bookBorrowService.userBorrowBook(userId,bookId);
        //获取最新的书籍信息
        List<Book> bookList = bookService.getBookList();
        HttpSession session = req.getSession();
        session.setAttribute("bookList",bookList);
        String contextPath = req.getContextPath();
        resp.sendRedirect(contextPath+"/user_index.jsp");

    }
}
